package com.mdz.sbgl.Controller;


import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Entity.*;
import com.mdz.sbgl.Mapper.SbwhbMapper;
import com.mdz.sbgl.Mapper.SbxxbMapper;
import com.mdz.sbgl.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/sbgl/sbwhb")
public class SbwhbController {

    @Autowired
    private SbwhbMapper sbwhbMapper;

    @Autowired
    private SbxxbMapper sbxxbMapper;
    @ResponseBody
    @RequestMapping(value = "/insertsbwh",method = RequestMethod.POST)
    public int insertsbwh(Sbwhb sbwhb){
        LocalDate today = LocalDate.now();
        LocalTime time=LocalTime.now();
        Integer Hour = time.getHour();
        Integer Minute = time.getMinute();
        Integer Second = time.getSecond();
        Integer Nano = Integer.parseInt(Integer.toString(time.getNano()).substring(0,3));
        Integer Year = today.getYear();
        Integer Day = today.getDayOfMonth();
        Integer Month = today.getMonthValue();
        Integer Months=0;
        if (Month<10){
            Months=Integer.parseInt("0"+Month);
        }
        String whjlbh=sbwhb.getGsid()+Year+Months+Day+Hour+Minute+Second+Nano+"";
        sbwhb.setWhjlbh(whjlbh);
        String ybm = sbwhb.getYbm();
        String bm=sbwhb.getBm();
        String yshiyr=sbwhb.getYshiyr();
        String shiyr=sbwhb.getShiyr();
        if (sbwhb.getWhjl().contains("使用部门")==true){
            sbwhb.setGzyy("部门或使用人变更");
            sbwhb.setJjfa("部门由"+ybm+"变更为"+bm+"使用人由"+yshiyr+"变更为"+shiyr);
        }
        return sbwhbMapper.insert(sbwhb);
    }

    @ResponseBody
    @RequestMapping(value = "/insertsbwhforsj",method = RequestMethod.POST)
    public int insertsbwhforsj(Sbwhb sbwhb){
        LocalDate today = LocalDate.now();
        LocalTime time=LocalTime.now();
        Integer Hour = time.getHour();
        Integer Minute = time.getMinute();
        Integer Second = time.getSecond();
        Integer Nano = Integer.parseInt(Integer.toString(time.getNano()).substring(0,3));
        Integer Year = today.getYear();
        Integer Day = today.getDayOfMonth();
        Integer Month = today.getMonthValue();
        Integer Months=0;
        if (Month<10){
            Months=Integer.parseInt("0"+Month);
        }
        String whjlbh=sbwhb.getGsid()+Year+Months+Day+Hour+Minute+Second+Nano+"";
        sbwhb.setWhjlbh(whjlbh);
        String ybm = sbwhb.getYbm();
        String bm=sbwhb.getBm();
        String yshiyr=sbwhb.getYshiyr();
        String shiyr=sbwhb.getShiyr();
        return sbwhbMapper.insert(sbwhb);
    }

    @ResponseBody
    @RequestMapping(value = "/getwhjllist",method = RequestMethod.GET)
    public String getwhjllist(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,@RequestParam(value = "kssj",required = false,defaultValue = "") String kssj,@RequestParam(value = "jssj",required = false,defaultValue = "") String jssj, @RequestParam(value = "gsid",required = false,defaultValue = "") int gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<Sbwhb> ncList=sbwhbMapper.selectList(new QueryWrapper<Sbwhb>().between("whrq",kssj,jssj).eq("gsid",gsid));
        PageInfo page=new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @GetMapping(value = "/getsbwhb/{gsid}/{kssj}/{jssj}")
    public List<Sbwhb> getsbwhb(@PathVariable("gsid") String gsid, @PathVariable("kssj") String kssj, @PathVariable("jssj") String jssj) {
        List<Sbwhb> sbwhbs = sbwhbMapper.selectList(new QueryWrapper<Sbwhb>().eq("gsid",gsid).between("whrq",kssj,jssj));
        return sbwhbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getdelwhjl",method = RequestMethod.GET)
    public String getdelwhjl(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,@RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj, @RequestParam(value = "gsid",required = false,defaultValue = "") String gsid){
//        response.setContentType("text/json");
//        response.setCharacterEncoding("utf-8");
//        List<Sbwhb> ncList=sbwhbMapper.selectList(new QueryWrapper<Sbwhb>().eq("gsid",gsid).and(wrapper-> wrapper.like("sbbh",cxtj).or().like("whlb",cxtj).or().like("gzyy",cxtj).or().like("jjfa",cxtj)));
//        List<Sbwhb> rows=new ArrayList<>();
//        System.out.println("控制器中的pageNumber值为："+pageNumber);
//        System.out.println("控制器中的pageSize值为："+pageSize);
//        return PageUtil.getpagestr(pageNumber,pageSize,ncList,rows);
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<Sbwhb> ncList=sbwhbMapper.getwhjl(gsid,cxtj);
//        List<Sbwhb> ncList=sbwhbMapper.selectList(new QueryWrapper<Sbwhb>().eq("gsid",gsid).and(wrapper-> wrapper.like("sbbh",cxtj).or().like("whlb",cxtj).or().like("gzyy",cxtj).or().like("jjfa",cxtj)));
        PageInfo page=new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();


    }

    @ResponseBody
    @RequestMapping(value = "/getwhjlstr",method = RequestMethod.GET)
    public String getwhjlstr(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,@RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj, @RequestParam(value = "gsid",required = false,defaultValue = "") int gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        List<Sbwhb> ncList=sbwhbMapper.selectList(new QueryWrapper<Sbwhb>().eq("gsid",gsid).and(wrapper-> wrapper.like("sbbh",cxtj).or().like("whlb",cxtj).or().like("gzyy",cxtj).or().like("jjfa",cxtj)));
        List<Sbwhb> rows=new ArrayList<>();
        System.out.println("控制器中的pageNumber值为："+pageNumber);
        System.out.println("控制器中的pageSize值为："+pageSize);
        return PageUtil.getpagestr(pageNumber,pageSize,ncList,rows);
//        PageHelper.startPage(pageNumber, pageSize);  ///.like("sbbh",cxtj)
//        List<Sbwhb> ncList=sbwhbMapper.selectList(new QueryWrapper<Sbwhb>().eq("gsid",gsid).and(wrapper-> wrapper.like("sbbh",cxtj).or().like("whlb",cxtj).or().like("gzyy",cxtj).or().like("jjfa",cxtj)));
//        PageInfo page=new PageInfo(ncList);
//        List<Sbxxb> rows = page.getList();
//        int total = (int)page.getTotal();
//        JSONObject result = new JSONObject();
//        result.put("rows",rows);
//        result.put("total",total);
//        return result.toJSONString();
    }

    @PostMapping("/remove/{id}")
    @ResponseBody
    public Integer remove(@PathVariable("id") String id)
    {
        return sbwhbMapper.delete(new QueryWrapper<Sbwhb>().eq("id",id));
    }

    @PostMapping("/delbaof/{id}/{gsid}/{sbbh}")
    @ResponseBody
    public Integer delbaof(@PathVariable("id") String id,@PathVariable("gsid") String gsid,@PathVariable("sbbh") String sbbh)
    {
        int delwh = sbwhbMapper.delete(new QueryWrapper<Sbwhb>().eq("id",id));
        if (delwh>=1){
            Sbxxb sbxxb=new Sbxxb();
            sbxxb.setBfbs("0");
            sbxxbMapper.update(sbxxb,new QueryWrapper<Sbxxb>().eq("sbbh",sbbh).eq("gsid",gsid));
        }
        return 1;
    }

}

